﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

using XJX.BusinessLayer;

namespace XJX.Page
{
    /// <summary>
    ///Books 的摘要说明
    /// </summary>
    public class Book
    {
        public Book()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        public static bool Add_New_Book
            (
            string bname,
            string author,
            string press,
            decimal price,
            string isbn,
            string pym,
            string cname,
            string descript,
            string pname,
            string uid,
            out int ReturnValue,
            out string ReturnFieldValue
            )
        {
            bool b;

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Book bs = new XJX.BusinessLayer.Book(str);

            try
            {
                b = bs.Add_New_Book(bname, author, press, price, isbn, pym, cname, descript, pname, uid, out ReturnValue, out ReturnFieldValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return b;
        }

        public static bool Modify_One_Book_Info
            (
            string bname,
            string author,
            string press,
            decimal price,
            string isbn,
            string pym,
            string cname,
            string descript,
            string pname,
            string uid,
            string bid,
            out int ReturnValue
            )
        {
            bool b;

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Book bs = new XJX.BusinessLayer.Book(str);

            try
            {
                b = bs.Modify_One_Book_Info(bname, author, press, price, isbn, pym, cname, descript, pname, uid,bid, out ReturnValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return b;
        }

        //获取一个家庭的所有图书
        public static string Get_One_Family_Book_From_UID(string UID, int PageIndex, int PageSize)
        {
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Book bs = new XJX.BusinessLayer.Book(str);

            Hashtable ht = bs.Get_One_Family_Book_From_UID(UID, PageIndex, PageSize);
            return XJX.BusinessLayer.JSON.Encode(ht);
        }

        //获取一个家庭的所有图书
        public static DataTable Get_One_Family_Book_From_UID(string UID)
        {
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Book bs = new XJX.BusinessLayer.Book(str);

            DataTable dt = bs.Get_One_Family_Book_From_UID(UID);
            return dt;
        }

        //得到某个分类图书的列表
        public static string Search_Book_List_From_CategoryID(string cname,string UID, int PageIndex, int PageSize)
        {
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Book bs = new XJX.BusinessLayer.Book(str);

            Hashtable ht = bs.Search_Book_List_From_CategoryID(cname,UID,PageIndex, PageSize);
            return XJX.BusinessLayer.JSON.Encode(ht);
        }


        //查询图书关键字，获得图书列表
        public static string Search_Book_List_From_BookName(string bname,string UID, int PageIndex, int PageSize)
        {
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Book bs = new XJX.BusinessLayer.Book(str);

            Hashtable ht = bs.Search_Book_List_From_BookName(bname,UID,PageIndex, PageSize);
            return XJX.BusinessLayer.JSON.Encode(ht);
        }


        //下架或上架一本书
        public static bool Shelf_One_Book_From_BID
            (
            string uid,
            string bid,
            out int ReturnValue,
            out string ReturnFieldValue
            )
        {
            bool b;

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Book bs = new XJX.BusinessLayer.Book(str);

            try
            {
                b = bs.Shelf_One_Book_From_BID(uid, bid, out ReturnValue, out ReturnFieldValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return b;
        }


        //获取一本书的相关信息
        public static string Get_One_Book_Info_From_BID(string BID)
        {
            string result = "";

            Hashtable ht = new Hashtable();
            DataTable dt = new DataTable();

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Book bs = new XJX.BusinessLayer.Book(str);

            try
            {
                dt = bs.Get_One_Book_Info_From_BID(BID);//得到内存数据表

                ArrayList data = Common.DataTable2ArrayList(dt);//将上述表的数据转换成数组表形式，单行数据是哈希表格式。

                ht["rows"] = data;//将上述数据再放入一个哈希表中。

                result = XJX.BusinessLayer.JSON.Encode(ht);//将上述哈希表进行格式化
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return result;
        }


        //获取一本书的相关信息
        public static DataRow Get_One_Book_Info_From_BID_1(string BID)
        {
            DataTable dt = new DataTable();

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Book bs = new XJX.BusinessLayer.Book(str);

            try
            {
                dt = bs.Get_One_Book_Info_From_BID(BID);//得到内存数据表
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return dt.Rows[0];
        }

        //得到某个家庭的下架书
        public static string Get_Off_Shelf_Books_From_UID(string UID, int PageIndex, int PageSize)
        {
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Book bs = new XJX.BusinessLayer.Book(str);

            Hashtable ht = bs.Get_Off_Shelf_Books_From_UID(UID, PageIndex, PageSize);
            return XJX.BusinessLayer.JSON.Encode(ht);
        }

        //得到某个家庭的上架书
        public static string Get_ON_Shelf_Books_From_UID(string UID, int PageIndex, int PageSize)
        {
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Book bs = new XJX.BusinessLayer.Book(str);

            Hashtable ht = bs.Get_ON_Shelf_Books_From_UID(UID, PageIndex, PageSize);
            return XJX.BusinessLayer.JSON.Encode(ht);
        }
    }




/////////////////////////////////////////////////////////////////////////////以下进行图书信息类属性定义
    
    public class BookInfo
    {
        int ID;
        string BID;
        string Bname;
        string Author;
        string Press;
        decimal Price;
        string ISBN;
        string PYM;
        string  CID;
        string Descript;
        string Pname;
        DateTime Cdate;
        string UID;
        int Cnum;
        int Bnum;
        string Cstatus;
        string Flag;
    }
}